/*
*	Project: Aethon
*
*	File: CollisionSphere.cpp
*
*	Author: Jonathan Hersack
*
*	Date: 2/15/2009
*
*   Purpose: The CollisionSphere contains the relative postion and radius of the sphere 
*   from the model coordinates. It also contains flags for the visibilty and collision
*/

#include "../include/CollisionSphere.h"
#include <math.h>
#include <GL/glut.h>

void CollisionSphere::display()
{  
   glPushMatrix();
   glColor3f(0/255.0,0/255.0,255/255.0);
//   glColor3f(255/255.0,0/255.0,0/255.0);
   glTranslatef(relativePosition.v[0],relativePosition.v[1],relativePosition.v[2]);
  
   //This makes it translucent :)
   glDepthMask(GL_FALSE);
   glEnable (GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); 
   glutSolidSphere(radius, 50, 50);
   glDisable (GL_BLEND);
   glDepthMask(GL_TRUE);
   
   glPopMatrix();
   
}//end display

int CollisionSphere::collidesWith(CollisionSphere sphere)
{
    
	vector3f dist;
	dist.v[0] = sphere.getRelativePosition().v[0]+sphere.getCenterPosition().v[0] - (relativePosition.v[0]+centerPosition.v[0]);
    dist.v[1] = sphere.getRelativePosition().v[1]+sphere.getCenterPosition().v[1] - (relativePosition.v[1]+centerPosition.v[1]);
	dist.v[2] = sphere.getRelativePosition().v[2]+sphere.getCenterPosition().v[2] - (relativePosition.v[2]+centerPosition.v[2]);
	
	float distance =  sqrt(dist.v[0]*dist.v[0]+dist.v[1]*dist.v[1]+dist.v[2]*dist.v[2]);
	
	if (distance <= (sphere.getRadius()+radius))
	{//collision
	return 1;//collision occured
	}//end if
	else 
	return 0;//no collision
	
}//end collidesWith

int CollisionSphere::collidesWith(vector3f point)
{
	
	vector3f dist;
	dist.v[0] = point.v[0] - (relativePosition.v[0]+centerPosition.v[0]);
    dist.v[1] = point.v[1] - (relativePosition.v[1]+centerPosition.v[1]);
	dist.v[2] = point.v[2] - (relativePosition.v[2]+centerPosition.v[2]);
	
	float distance =  sqrt(dist.v[0]*dist.v[0]+dist.v[1]*dist.v[1]+dist.v[2]*dist.v[2]);
	
	if (distance <= radius)
	{//collision
	return 1;//collision occured
	}//end if
	else 
	return 0;//no collision

}//end collidesWith
